package com.qf.game.utils;

import java.util.concurrent.*;

/**
 * 线程池的工具类
 *
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
public class ThreadUtils {

    private static ExecutorService executorService;

    static {
        //读取一下CPU的核心数
        int cores = Runtime.getRuntime().availableProcessors();
        System.out.println("cpu的核心数：" + cores);

        //初始化线程池
        executorService = new ThreadPoolExecutor(
                cores,
                cores * 2,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10000),
                Executors.defaultThreadFactory(),
                //拒绝策略 - 谁提交的任务由谁自己来执行
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        System.out.println("线程池初始化完成！");
    }

    /**
     * 提交任务给线程池
     * @param runnable
     */
    public static void submit(Runnable runnable){
        executorService.submit(runnable);
    }
}
